package com.xudow.app.newui.dialog;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xudow.app.R;
import com.xudow.app.newui.zutils.ChooseWheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zhouXJ on 2016/4/14 13:43
 * 性别选择对话框
 */
public class SexChoicePop {

    private static SexChoicePop sexChoicePop;

    public interface SexChoicePopListner {
        void onOkClick(String sexStr);
    }

    private SexChoicePopListner okClickListner;

    public void setOkClickListner(SexChoicePopListner okClickListner) {
        this.okClickListner = okClickListner;
    }


    private ChooseWheelView sexView;

    private List<String> sexList;


    private TextView sex_pop_cancel_btn;
    private TextView sex_pop_sure_btn;
    private PopupWindow popupWindow;

    public static SexChoicePop getInstance() {
        if (sexChoicePop == null) {
            sexChoicePop = new SexChoicePop();
        }
        return sexChoicePop;
    }


    public void showPopupWindow(View view, final Activity context, String curSex) {


        View popView = LayoutInflater.from(context).inflate(
                R.layout.sex_layout, null);

        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(R.style.exit_pop);
        popupWindow.setBackgroundDrawable(null);

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = context.getWindow().getAttributes(); // 设置窗体透明度
        lp.alpha = 0.3f;
        context.getWindow().setAttributes(lp);

        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        sex_pop_cancel_btn = (TextView) popView.findViewById(R.id.sex_pop_cancel_btn);
        sex_pop_sure_btn = (TextView) popView.findViewById(R.id.sex_pop_sure_btn);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                dismissPopWindow(context);
            }
        });



        sex_pop_cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissPopWindow(context);
            }
        });

        sex_pop_sure_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (okClickListner != null) {
                    String sexStr = sexView.getSeletedItem();
                    okClickListner.onOkClick(sexStr);
                    dismissPopWindow(context);
                }
            }
        });


        initSexList();  // 初始化年
        sexView = (ChooseWheelView) popView.findViewById(R.id.sex);
        sexView.setItems(sexList);
        sexView.setOffset(1);

        for (int i = 0; i < sexList.size(); i++) {
            if (sexList.get(i).equals(curSex)) {
                sexView.setSeletion(i);
                break;
            }
        }

        sexView.setOnWheelViewListener(new ChooseWheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                sexView.getSeletedItem();
            }
        });
    }

    /**
     * 隐藏pop
     *
     * @param activity
     */
    public void dismissPopWindow(Activity activity) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;

        }
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); // 恢复窗体透明度
        lp.alpha = 1.0f;
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 是否显示
     *
     * @return
     */
    public boolean isShow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 初始化
     */
    public void initSexList() {
        sexList = new ArrayList<String>(2);
        sexList.add("女");
        sexList.add("男");
    }

}
